/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/
#ifndef TRACKTABLE_H
#define TRACKTABLE_H

#include "gps_point.h"
#include "gps_waypoint.h"
#include "gpsmanager.h"

class TracksManager;
class MapsManager;
class LiveTrack;

class FTSlideButton;
class FTCompass;
class FTPushButtonIcon;
class FTLabel;
class FTTextArea;
class FTLatLonEdit;
class FTHelpWidget;

class TrackTable : public QWidget
{
    Q_OBJECT
public:
    explicit TrackTable(const TracksManager*,
                        LiveTrack*,
                        const MapsManager* );
    ~TrackTable();

    // Paint the table...
    void paintEvent ( QPaintEvent * event );

    // Manage mouse events, to support for dragging the table around!
    void mousePressEvent ( QMouseEvent * event );
    void mouseReleaseEvent ( QMouseEvent * event );
    void mouseMoveEvent ( QMouseEvent * event );

    // Reposition all the widgets when the screen is rotated
    void resizeEvent(QResizeEvent *);

signals:
    // Emitted when the currently set destination has been reached
    void destinationReached();
    // Emitted when the user stop the following mode
    void navigationStopped();
    // Emitted when the user click on the tracking button
    void toggleTracking();
    // Emitted when the user click on the show map button
    void toggleMaps();
    // Emitted when the use click on the gps button
    void toggleGps();

public slots:
    // Notify the table that a new point has been received from the GPS
    void gpsUpdated(GPS_point);
    // Notify the table that the GPS has lost/gained the fix
    void gpsStatusChanged(GpsManager::gps_status);
    // Notify the table if maps are to be shown or hidden (quickie to avoid to call a function in MapsMAnager at each update)
    void mapsEnabled(bool);
    // Notify me when livetrack status has changed
    void livetrackRecording(bool);
    // Notify the table that a destination has been set
    void startNavigation( QGeoCoordinate );
    // Force to stop currently set destination.
    void stopNavigation();

private slots:
    // Zoom the map...
    void zoomIn();
    void zoomOut();
    // Enable the table to stay in sync with the latest GPS position
    void followMode();
    // Add a waypoint (user request)
    void addWaypoint();
    // Set modes:
    void setMode(int);
    // User is manually chaning the lat/lon
    void updateLatLon(double, double);
    void updateAltitude(double);
    // Receive ebaring changes from compass
    void bearingUpdate(qreal);

    // Proximity updated:
    void updateProximity(int);

private:
    LiveTrack* livetrack;
    // Keep track of layers
    const TracksManager* tracks;
    // Keep track of maps
    const MapsManager* maps;
    // Quickie to draw or hide the maps
    bool drawMaps;
    // Keep track of the latest GPS update
    QGeoCoordinate gps_position;
    double current_speed;
    double current_steepness;

    void buildCommonWidgets();
    void buildNavigationWidgets();
    void buildFullWidgets();

    void toggleNavigation(bool);
    void toggleFull(bool);

    // Quickie. Is gps fixed?
    GpsManager::gps_status gps_has_fix;

    // Always visible controls:
    FTPushButtonIcon* zoomInButton;
    FTPushButtonIcon* zoomOutButton;
    FTSlideButton* modeButton;
    FTLatLonEdit* latLonLabel;
    QPixmap centerPixmap;
    QPixmap zoomMarkerPixmap;
    QPixmap zoomMeterPixmap;

    // Navigation mode controls and stuff:
    FTPushButtonIcon * follow;
    FTPushButtonIcon * waypoint;
    FTPushButtonIcon * stopNavigationButton;
    FTTextArea* altitudeLabel;
    FTCompass* compass;
    QPixmap gpsPositionPixmap;
    QPixmap gpsPosNoFixPixmap;
    QPixmap comeHerePixmap;
    QPolygon bearingHandPoly;
    QPolygon navigationHandPoly;

    // Full controls mode:
    FTPushButtonIcon* startTrackButton;
    FTPushButtonIcon* enableMapsButton;
    FTPushButtonIcon* enableGpsButton;

private: // track position and scaling...
    void updateMatrix();

    QTransform wgs84_to_screen_matrix;
    QRectF wgs84_visible_rect;
    double longitude_correction;
    double screen_scale;
    int zoom_step;

    // Track current screen center (offset-factor)
    double screen_latitude;
    double screen_longitude;
    int screen_altitude;

    // Also we need this to track mouse-related tracking when panning
    QPointF mouse_start;
    qreal currentBearing;

private: // Navigation data
    // currently set destination
    QGeoCoordinate destination;
    // Is navigation enabled?
    bool navigationEnabled;
    // destination has been reached!
    void reachedDestination();
    double proximity_threeshold;
    long int destination_distance;

private: // modes and widget placement data
    int current_mode;
    int button_height;
    int button_width;
    int zoom_start_y;
    QPoint gps_pin_offset;
    QPoint comeHereOffset;
};

#endif // TRACKTABLE_H
